<?php

namespace App\Console\Commands;

use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use App\Http\Model\Mall;
use App\Http\Services\MsgServices;
use PDO;

class CrossRoomPkSendDayily extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'CrossRoomPkSendDayily';

    protected $crossRoomPkStream = 'cross_room_pk_stream';//房间流水pk
    protected $room = 'room';
    protected $user_table = 'user';

    protected $log = 'cross_room_pk_send_day';//日志
    protected $error_log = 'error_cross_room_pk_day';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '每日结算用户日榜,房间日榜前三';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {  
        $yestordayStart = Carbon::yesterday()->startOfDay()->toDateTimeString();
        $yestordayEnd = Carbon::yesterday()->endOfDay()->toDateTimeString();

        do{
            //查询用户日榜前三名
            $usermodel = DB::table($this->crossRoomPkStream)
                    ->select(DB::raw('sum(point+award_point) as `point`,from_user_id,room_id'))
                    ->where("created_at",'>=',$yestordayStart)
                    ->where("created_at",'<=',$yestordayEnd)
                    ->groupBy('from_user_id');
            //重新分页统计好的数据
            $useRres = DB::table(DB::raw("({$usermodel->toSql()}) as other"))
                ->mergeBindings($usermodel)
                ->orderBy('point','desc')
                ->limit(3)
                ->get();
            $useRres = objToArray($useRres);
            //用户榜发奖励
            if(!empty($useRres)){
                $this->sendUserDay($useRres);
            }else{
                write_log($this->log,'INFO 无数据',$useRres);
            }
            //房间日榜发放
            $rooMmodel = DB::table($this->crossRoomPkStream)
                    ->select(DB::raw('sum(room_point) as `point`,room_id'))
                    ->where("created_at",'>=',$yestordayStart)
                    ->where("created_at",'<=',$yestordayEnd)
                    ->groupBy('room_id');
            //重新分页统计好的数据
            $roomRes = DB::table(DB::raw("({$rooMmodel->toSql()}) as other"))
                ->mergeBindings($rooMmodel)
                ->orderBy('point','desc')
                ->limit(3)
                ->get();
            $roomRes = objToArray($roomRes);
            if(!empty($roomRes)){
                $this->sendRoomDay($roomRes);
            }else{
                write_log($this->log,'INFO 无数据',$roomRes);
            }
        }while(false);
    }

    //发放房间日榜奖励
    public function sendRoomDay($roomRes){
        //给前三名房间发送奖励
        foreach($roomRes as $k => $v){
            //查询房主user_id
            $roomInfo = DB::table($this->room)->where('room_id',$v['room_id'])->first();
            $roomInfo = objToArray($roomInfo);
            if($k == 0){
                $giftArr = [
                    '143'=>1,
                ];
                $sendGoodsInfo = [
                    [
                        'goods_id'=>79,
                        'day'=> 30,
                    ],
                    [
                        'goods_id'=>125,
                        'day'=> 30,
                    ],
                    [
                        'goods_id'=>113,
                        'day'=> 30,
                    ],
                ];
                $this->_userSendGood($roomInfo['user_id'],$sendGoodsInfo);
                $msg = '恭喜您在昨日跨房PK中房间贡献榜第一名,奖励已经下发到背包,请查收!';
                $this->_userSendMsg($roomInfo['user_id'],$msg);
            }elseif($k ==1 ){
                $giftArr = [
                    '144'=>1,
                ];
                $sendGoodsInfo = [
                    [
                        'goods_id'=>78,
                        'day'=> 30,
                    ],
                    [
                        'goods_id'=>126,
                        'day'=> 30,
                    ],
                    [
                        'goods_id'=>113,
                        'day'=> 15,
                    ],
                ];
                $this->_userSendGood($roomInfo['user_id'],$sendGoodsInfo);
                $msg = '恭喜您在昨日跨房PK中房间贡献榜第二名,奖励已经下发到背包,请查收!';
                $this->_userSendMsg($roomInfo['user_id'],$msg);
                
            }elseif($k == 2){
                //top3
                $giftArr = [
                    '145'=>1,
                ];
                $sendGoodsInfo = [
                    [
                        'goods_id'=>77,
                        'day'=> 30,
                    ],
                    [
                        'goods_id'=>127,
                        'day'=> 30,
                    ],
                    [
                        'goods_id'=>113,
                        'day'=> 7,
                    ],
                ];
                $this->_userSendGood($roomInfo['user_id'],$sendGoodsInfo);
                $msg = '恭喜您在昨日跨房PK中房间贡献榜第三名,奖励已经下发到背包,请查收!';
                $this->_userSendMsg($roomInfo['user_id'],$msg);
            }
        }
    }

    //发放用户贡献榜
    public function sendUserDay($useRres){
        //给前三名用户发送奖励
        foreach($useRres as $k => $v){
            if($k == 0){
                $giftArr = [
                    '143'=>1,
                ];
                $sendGoodsInfo = [
                    [
                        'goods_id'=>79,
                        'day'=> 30,
                    ],
                    [
                        'goods_id'=>125,
                        'day'=> 30,
                    ],
                    [
                        'goods_id'=>113,
                        'day'=> 30,
                    ],
                ];
                $this->_userSendGood($v['from_user_id'],$sendGoodsInfo);
                $msg = '恭喜您在昨日跨房PK中贡献榜第一名,奖励已经下发到背包,请查收!';
                $this->_userSendMsg($v['from_user_id'],$msg);
            }elseif($k ==1 ){
                $giftArr = [
                    '144'=>1,
                ];
                $sendGoodsInfo = [
                    [
                        'goods_id'=>78,
                        'day'=> 30,
                    ],
                    [
                        'goods_id'=>126,
                        'day'=> 30,
                    ],
                    [
                        'goods_id'=>113,
                        'day'=> 15,
                    ],
                ];
                $this->_userSendGood($v['from_user_id'],$sendGoodsInfo);
                $msg = '恭喜您在昨日跨房PK中贡献榜第二名,奖励已经下发到背包,请查收!';
                $this->_userSendMsg($v['from_user_id'],$msg);
                
            }elseif($k == 2){
                //top3
                $giftArr = [
                    '145'=>1,
                ];
                $sendGoodsInfo = [
                    [
                        'goods_id'=>77,
                        'day'=> 30,
                    ],
                    [
                        'goods_id'=>127,
                        'day'=> 30,
                    ],
                    [
                        'goods_id'=>113,
                        'day'=> 7,
                    ],
                ];
                $this->_userSendGood($v['from_user_id'],$sendGoodsInfo);
                $msg = '恭喜您在昨日跨房PK中贡献榜第二名,奖励已经下发到背包,请查收!';
                $this->_userSendMsg($v['from_user_id'],$msg);
            }
        }
    }

    //用户批量发礼物
    public function _userSendGood($userId, $goodArrs)
    {
        
        $mall_model = new Mall();

        $goodMap = [];
        foreach($goodArrs as $good) {
            $goodId = $good['goods_id'];
            $goodInfo = $mall_model->getMallGoodByGoodsId($goodId);
            if(empty($goodInfo)) {
                echo '商品不存在'.$goodId;
                exit;
            }
            $skuList = $mall_model->getSkuDataByGoods($goodId);
            if(empty($skuList[0])) {
                echo '商品不存在'.$goodId;
                exit;
            }
            $goodMap[$goodId]['goods_name'] = $goodInfo['goods_name'];
            $goodMap[$goodId]['goods_type'] = $goodInfo['goods_type'];
            $goodMap[$goodId]['sku_id'] = $skuList[0]['sku_id'];
        }
        foreach($goodArrs as $goods) {
            //有效期
            $timeLimit = $goods['day']*86400;
            //商品增加
            $mall_model->shoppingAct(
                11,
                $userId,
                $goods['goods_id'],
                $goodMap[$goods['goods_id']]['goods_name'],
                $goodMap[$goods['goods_id']]['goods_type'],
                $goodMap[$goods['goods_id']]['sku_id'],
                $timeLimit,
                0,
                $timeLimit,
                time(),
                2
            );
            write_log($this->log ,'INFO 商品赠送完成 用户 '.$userId.' 商品 '.$goodId['goods_id'],$goods);
        }
    }
    //用户批量发系统消息
    public function _userSendMsg($userId, $msg)
    {
        $msgServices = new MsgServices();
        $msgServices->send_sys_text_msg($userId, $msg);
        write_log($this->log ,'INFO 消息发送 用户 '.$userId,$msg);
    }
}